﻿Imports agsXMPP
Imports Microsoft.Win32
Public Class Form1
    Dim xmpp As agsXMPP.XmppClientConnection

    Dim frm As frmChat
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
        'Load settings
        'txtUsername.Text = My.Settings.UserName
        'txtPassword.Text = My.Settings.Password
    End Sub


    Private Sub loggedIn()
        Me.Hide()
        NotifyIcon.Visible = True
        lblStatus.Text = "Logged in and Active."
        NotifyIcon.Text = "Gtalk Autoreply Enabled"
    End Sub
    Private Sub loginFailed()
        Me.Show()
        lblStatus.Text = "Login failed. Please check your details."
    End Sub
    Private Sub messageReceived(ByVal sender As Object, ByVal msg As protocol.client.Message)
        frm = frmChat
        Debug.Print(msg.Body.ToString)
        'TODO: Some error while running occurs in the code below
        If msg.Chatstate <> protocol.extensions.chatstates.Chatstate.composing Then
            Dim chatMessage() As String
            chatMessage = msg.From.ToString.Split("/")
            'frm.txtJid.Text = chatMessage(0)
            'frm.txtMsgs.Text = frm.txtMsgs.Text & vbCrLf & chatMessage(0) & ">>" & msg.Body
            Dim jid As agsXMPP.Jid
            jid = New agsXMPP.Jid(chatMessage(0))
            Dim autoReply As protocol.client.Message
            autoReply = New protocol.client.Message(jid, protocol.client.MessageType.chat, "jhgfjhgjj. beta software")
            xmpp.Send(autoReply)
            'frm.Show()
        End If
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If txtUsername.Text.Trim = "" Then
            lblStatus.Text = "Please enter Username."
            txtUsername.Focus()
            Exit Sub
        End If

        If txtPassword.Text.Trim = "" Then
            lblStatus.Text = "Please enter Password."
            txtPassword.Focus()
            Exit Sub
        End If
        lblStatus.Text = "Logging in... pls wait"
        xmpp = New agsXMPP.XmppClientConnection
        Dim jid As agsXMPP.Jid
        jid = New agsXMPP.Jid(String.Format("{0}@gmail.com", txtUsername.Text.Trim))
        xmpp.Password = txtPassword.Text.Trim
        xmpp.Username = jid.User
        xmpp.Server = jid.Server
        xmpp.AutoResolveConnectServer = True
        Try
            AddHandler xmpp.OnMessage, AddressOf messageReceived
            AddHandler xmpp.OnLogin, AddressOf loggedIn
            AddHandler xmpp.OnAuthError, AddressOf loginFailed
            xmpp.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
        Dim frm As frmChat
        frm = New frmChat
        frm.Show()
    End Sub

    Private Sub NotifyIcon_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon.DoubleClick
        Me.Show()
    End Sub
End Class
